import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Demo {
    FileSystem fs=null;
    int depth = 0;
    int dCount = 0;
    int fCount = 0;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // 1. 创建配置对象
        Configuration conf = new Configuration();
        // 2. 设置参数
        conf.set("fs.defaultFS", "hdfs://192.168.142.111:9000");
        // 3. 设置块大小为100MB
        conf.set("dfs.blocksize", "104857600");

        // 4. 使用正确的URI和Configuration获取FileSystem实例
       fs = FileSystem.get(new URI("hdfs://192.168.142.111:9000"), conf, "root");

    }

    public void listFilesRecursively(String dirPath) throws IOException {
        Path path = new Path(dirPath);

        if (fs.exists(path)) {
            FileStatus[] statuses = fs.listStatus(path);
            for (FileStatus status : statuses) {

                if (status.isDirectory()) {
                    System.out.println("----------------");

                    System.out.println("文件夹: " + status.getPath());
                    System.out.println("深度: " + (++depth));
                    System.out.println("文件夹计数: " + ++dCount);
                    // 递归调用以遍历子目录
                    listFilesRecursively(status.getPath().toString());
                } else {
                    System.out.println("----------------");
                    System.out.println("文件: " + status.getPath());
                    System.out.println("文件计数: " + ++fCount);
                }
            }
        } else {
            System.out.println("这个文件夹不存在.");
        }
    }

    @Test
    public void test10() throws IOException {
        //递归遍历文件夹
        String dirPath = "/";
        listFilesRecursively(dirPath);
    }

    @Test
    public void test1() throws IOException {
        //创建文件夹
        Path newdir = new Path("/a/b/c");
        boolean flag = fs.mkdirs(newdir);
        if (flag) {
            System.out.println("创建成功"+newdir);
        } else {
            System.out.println("创建失败"+newdir);
        }
    }

    @Test
    public void test() throws IOException {
        //创建文件，并写入内容
        Path path = new Path("/newdir/demo/test/a.txt");
        FSDataOutputStream out = fs.create(path);
        String content = "Hello, Hadoop!";
        out.write(content.getBytes());
        out.close();
    }
    @Test
    public void test9() throws IOException {
        //下载文件
        Path src = new Path("/newdir/demo/test/a.txt");
        Path dst = new Path("e:\\b.txt");
        fs.copyToLocalFile(false, src, dst, false);
        System.out.println("文件下载成功: " + src + " -> " + dst);
    }

    @Test
    public void test2() throws IOException {
        //删除文件
        Path path = new Path("/newdir/demo");
        boolean flag = fs.delete(path, true);//true表示递归删除
        if (flag) {
            System.out.println("删除成功"+path);
        } else {
            System.out.println("删除失败"+path);
        }
    }

    @Test
    public void test3() throws IOException {
        //重命名文件
        Path path = new Path("/newdir/demo/test");
        Path path1 = new Path("/newdir/demo/test1");
        boolean flag = fs.rename(path, path1);
        if (flag) {
            System.out.println("重命名成功"+path1);
        } else {
            System.out.println("重命名失败"+path1);
        }
    }
    @Test
    public void test4() throws IOException {
        //判断文件是否存在
        Path path = new Path("/newdir/demo/test1");
        boolean flag = fs.exists(path);
        if (flag) {
            System.out.println("文件存在"+path);
        } else {
            System.out.println("文件不存在"+path);
        }
    }

    @Test
    public void test5() {
        //获取文件状态
        Path path = new Path("/newdir/demo/test1");
        try {
            FileStatus status = fs.getFileStatus(path);
            System.out.println("文件长度: " + status.getLen());
            System.out.println("是否是目录: " + status.isDirectory());
        } catch (IOException e) {
            System.err.println("获取文件状态失败: " + e.getMessage());
        }
    }

    @Test
    public void test6() throws IOException {
        //列出文件内容，使用listFiles
        Path path = new Path("/newdir/demo");
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(path, true);
        while (files.hasNext()) {
            LocatedFileStatus fileStatus = files.next();
            System.out.println("文件名: " + fileStatus.getPath().getName());
        }
    }
    @Test
    public void test7() throws IOException {
        //列出文件内容，使用listStatus
        Path path = new Path("/newdir/demo");
        FileStatus[] files = fs.listStatus(path);
        for (FileStatus status : files) {
            System.out.println("文件名: " + status.getPath().getName());
        }
    }
    @Test
    public void test8() throws IOException {
        //下载文件到本地
        Path src = new Path("/user/your_username/file_on_hdfs.txt");
        Path dst = new Path("e:\\a.txt");
        fs.copyToLocalFile(false, src, dst, false);
        System.out.println("文件下载成功: " + src + " -> " + dst);
    }
}
